const API = 'https://chengyu.xingliaoapp.com/';

const get = (cmd, params, callback) => {
    //params.token = wx.getStorageSync('token') || '';
    wx.showToast({
         title: '数据加载中...',
        icon: 'loading',
        duration: 2000
    })
    wx.request({
        url: API+cmd,
        data: params,
        success: (res) => {
            console.log("request->" + res.data);
            wx.hideToast();
            /*
            const data = res.data;
            if(data.code){
                wx.showModal({
                    title: '提示',
                    showCancel: false,
                    confirmColor: 'rgb(251,93,93)',
                    content: data.message,
                    complete: (res) => {
                        if(data.message.indexOf('认证失败') != -1 && params.token){
                           wx.clearStorageSync()
                        }
                    }
                    })
                return
            }*/
            if(typeof(callback) == 'function')
              callback(res);
              //callback(data.data);
        }
    })
}

const post = (cmd, params, callback,isLoding=true) => {
    //params.token = wx.getStorageSync('token') || ''
    if (isLoding){
      wx.showToast({
        title: '处理中...',
        icon: 'loading',
        duration: 2000
      });
    }
    wx.request({
        url: API+cmd,
        data: params,
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        method: 'POST',
        success: (res) => {
          wx.hideToast();
          console.log("request->" + res.data);
            /*
            const data = res.data
            if(data.code){
                wx.showModal({
                    title: '提示',
                    showCancel: false,
                    confirmColor: 'rgb(251,93,93)',
                    content: data.message,
                    success: (res) => {
                    }
                    })
                return
            }*/
            if(typeof(callback) == 'function')
                callback(res.data);
        }
    })
}

export default {
    get: get,
    post: post
}